package com.zz.springboot.processor.bag.strategy;

import com.zz.springboot.anno.StrategyType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class BagStrategyFactory {

    @Autowired
    private List<BagStrategy> strategies; //这里能将Strategy的全部实现类对象注入进来

    public final Map<String,BagStrategy> strategyMap = new HashMap<>();

    @PostConstruct
    public void initStrategyMap() {
/*      //这种方式不便于扩展，借助注解提高可扩展性
        for (BagStrategy strategy : strategies) {
            if (strategy instanceof BagOddsEvensStrategy) {
                strategyMap.put("oddEven", strategy);
            } else if (strategy instanceof BagAnimalsStrategy) {
                strategyMap.put("animal", strategy);
            } else if (strategy instanceof BagBigLittleStrategy) {
                strategyMap.put("bigLittle", strategy);
            } else if (strategy instanceof BagColorStrategy) {
                strategyMap.put("color", strategy);
            } else if (strategy instanceof BagHeadStrategy) {
                strategyMap.put("head", strategy);
            } else if (strategy instanceof BagTailStrategy) {
                strategyMap.put("tail", strategy);
            } else if (strategy instanceof BagColorOeStrategy) {
                strategyMap.put("colorOe", strategy);
            } else if (strategy instanceof BagBlOeStrategy) {
                strategyMap.put("blOe", strategy);
            } else if (strategy instanceof BagFiveElementsStrategy) {
                strategyMap.put("fiveElements", strategy);
            } else if (strategy instanceof BagPoultryWildStrategy) {
                strategyMap.put("poultryWild", strategy);
            }
            // 可以继续添加其他策略类型
        }
 */
        strategyMap.clear();
        for (BagStrategy strategy : strategies) {
            StrategyType typeAnnotation = strategy.getClass().getAnnotation(StrategyType.class);
            if (typeAnnotation != null) {
                strategyMap.put(typeAnnotation.value(), strategy);
            }
        }
    }

    public BagStrategy getStrategy(String type) {
        return strategyMap.get(type);
    }
}
